/**
 * @description Ccay XToolbar
 */
Ccay.UI.XToolbar = {};
(function(Toolbar){
	// set defalut config
	var config = {
		css : "ccay-toolbar", //默认toolbar样式，可自定义
		hasItemDiv : true,// 是否需要item div 包住项
		toolbarItem : "<div class='toolbar-item ${itemCSS}' ></div>",
		flow : false
	};
	
	//define control flow
	var flow = {
		init : function(toolbar, op){
			toolbar.dom.addClass("init").addClass(op.css);
			op.flow && toolbar.dom.addClass("toolbar-flow");
			op.width && toolbar.dom.css("width",op.width);
			op.height && toolbar.dom.css("height",op.height);
			op.operation || (op.operation = {});
			op._itemCount = 0;
		},
		build : function(toolbar, op){
			ccayToolbar.buildBtn(toolbar, op);
		},
		bindEvent:function(toolbar){
			//进行事件绑定
			toolbar.dom.on('click','[_eno]',function(e){
				var btn = $(this), eno = btn.attr("_eno");
				if(eno){
					var ev = toolbar.op.operation[eno];
					ev && ev.call(btn,e,toolbar);
				}
			});
		}
	};
	//define control manager
	var manager = {
		// 得到项 btnSelector 元素选择器
		getItem : function(btnSelector){ 
			if(this.op.hasItemDiv){
				return btnSelector.parent();
			}else{
				return btnSelector;
			}
		},
		// 添加项
        addItem: function(itemOp){
        	var fragment = document.createDocumentFragment();
        	ccayToolbar.addBtn(this,this.op,itemOp,fragment);
        	this.dom.append(fragment);
        },
        //  根据id移除项
        removeItem : function (btnSelector){
        	 this.getItem(btnSelector).remove();
        	 this.op._itemCount --;
        },
        // 隐藏toolbar或toolbaritem
        hide : function(btnSelector){
        	if(btnSelector){
        		this.getItem(btnSelector).hide();
        	}else{
        		return this.dom.hide();
        	}
        },
        // 显示toolbar或toolbaritem
        show : function(btnSelector){
        	if(btnSelector){
        		this.getItem(btnSelector).show();
        	}else{
        		return this.dom.show();
        	}
        },
        getItemCount : function(){
        	return this.op._itemCount;
        }
	};
	
	//create XControl
	var XToolbar = Ccay.UI.XControl("XToolbar", flow, config, manager);
	
	// create jquery plugin
	$.fn.ccayToolbar = function(op){
		this.length && new XToolbar (this, op);
		return this;
	};
	
	var ccayToolbar = {
		buildBtn:function(toolbar,op){
			if(op.items && op.items.length){
				var fragment = document.createDocumentFragment();
				$.each(op.items, function(i, btnOp){
					ccayToolbar.addBtn(toolbar, op, btnOp, fragment);
				});
				toolbar.dom.append(fragment);
			}
		},
		addBtn:function(toolbar,op,btnOp,container){
			if(btnOp.permission && !Ccay.Permission.check(btnOp.permission)){
				return;
			}
			op._itemCount ++;
			var item = toolbar.dom;
			// 是否需要item div 包住项
			if(op.hasItemDiv){
				item = $(op.toolbarItem.formatField(btnOp)).id("_item" + (op._itemCount));
				container.appendChild(item[0]);
			};
			// 可公共设置统一的按钮类型op.type
			btnOp.type = btnOp.type || op.type ;
			switch(btnOp.type){
				case "menuButton" :
		       	    var btn = $.create("div", "ccay-menubutton",btnOp.id).appendTo(item);
		       	    btn.ccayMenuButton(btnOp);
		       	    break;
		       	case "html" :
		       		item.append(btnOp.htmlContent);
		       	    break;
	       	    default :
	       	    	ccayToolbar.regEvent(op, btnOp);
	       	    	item.append(Ccay.UI.newButton(btnOp));
	       	    	$(".ccay-button.reset").click(function(){
	       	    		$(this).closest("form").ccayReset();
	       	    	});
					break;
			}
		},
		regEvent:function(op,btnOp){
			var ecode;
			// 操作code处理
			if(btnOp.opType){
				ecode = btnOp.opType;
			}else if(btnOp.click && typeof(btnOp.click) != "string"){
				ecode = btnOp.id || $.newGuid('toolbar_');
				op.operation[ecode] = btnOp.click;
				delete btnOp.click;
			}
			// attr处理
			btnOp.attr ? btnOp.attr._eno = ecode : btnOp.attr = {_eno:ecode};
		}
	}	
	
})(Ccay.UI.XToolbar);